package com.xcity.game.combat.skill.impl;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.game.combat.Action;
import com.xcity.game.combat.ActionEffect;
import com.xcity.game.combat.CombatUtils;
import com.xcity.game.combat.Round;
import com.xcity.game.combat.attr.CombatAttributeType;
import com.xcity.game.combat.buff.CombatBuff;
import com.xcity.game.combat.skill.CombatSkill;
import com.xcity.game.combat.unit.CombatUnit;
import com.xcity.game.common.GameStaticConfig;
import com.xcity.game.skill.Skill;

/**
 * 百试不爽<br/>
 * 随机挑选一个目标，若目标气势值高于自己，则造成{0=120#0}%技能伤害，并降低目标{1=50#0}%防御持续{2=2#0}回合
 * @author yang.li
 *
 */
public class COMBAT_SKILL_90090 extends CombatSkill {

	public COMBAT_SKILL_90090(Skill skill) {
		super(skill);
	}

	@Override
	public void calcDamage(Action action) {
		CombatUnit source = action.unit;
		CombatUnit target = action.target.getUnitRandomly();
		if (target == null) {
			return;
		}
		if (target.getHp() > source.getHp()) {
			// 计算伤害
			ActionEffect effect = action.addTarget(target);
			target = effect.getUnit(); // 修正目标
			float rate = skill.getParameter(0) / GameStaticConfig.percentBase;
//			int damage = (int) (source.getMagicPower() * rate - CombatUtils.calcMagicDefenseReduceDamage(target));
//			damage = CombatUtils.correctDamage(damage);
//			effect.effect(CombatAttributeType.HP, -damage);
			CombatUtils.calcMagicAttack(effect, rate);
			// 加buff
			action.addBuff(new BUFF(skill, target));
		} else {
			ActionEffect effect = action.addTarget(target);
			CombatUtils.calcMagicAttack(effect);
		}
	}
	
	static class BUFF extends CombatBuff {
		int oldDefense;
		public BUFF(Skill skill, CombatUnit owner) {
			super(skill.getTemplate().getCombatBuff(), owner, (int) skill.getParameter(2));
			active = true;
			int defense = (int) owner.getDefense();
			oldDefense = defense;
			defense -= defense * skill.getParameter(1) / GameStaticConfig.percentBase;
			owner.setAttributeValue(CombatAttributeType.DEFENSE, defense);
		}
		@Override
		public void roundEnd(Round round) {
			super.roundEnd(round);
			if (state == STATE_REMOVED) {
				owner.setAttributeValue(CombatAttributeType.DEFENSE, oldDefense);
			}
		}
		@Override
		@JSONField(serialize = false)
		public boolean isDebuff() {
			return true;
		}
	}

}
